package test.y2019.october;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author song
 * @date 2019/10/10 20:26
 */
public class October3 {

    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2019/10/10 20:26
     * Description   随机生成不重复的6或多位数字;存在redis
     */

  /*  private static final String SIX_NUM = "sixNum:";

    private String getKey(Integer newNum) {
        DateTime dateTime = new DateTime();
        Month month = dateTime.monthEnum();
        final int year = dateTime.year();
        return SIX_NUM + year + ":" + month.name() + ":" + newNum;
    }

    private Set<Integer> getOldNum() {
        String indistinctKey = String.format("%s*", SIX_NUM);
        Set<Integer> set = new HashSet<>();
        try (ShardedJedis resource = shardedJedisPool.getResource()) {
            Set<String> keys = resource.getShard("0").keys(indistinctKey);
            Iterator<String> iterator = keys.iterator();
            List<String> allKeys = IteratorUtils.toList(iterator);
            allKeys.forEach(i -> {
                String sixNum;
                try {
                    sixNum = i.split(":")[3];
                } catch (Exception ex) {
                    return;
                }
                set.add(Integer.valueOf(sixNum));
            });
        }
        return set;
    }

    private void saveNewNum(Integer newNum) {
        final String key = getKey(newNum);
        try (ShardedJedis resource = shardedJedisPool.getResource()) {
            String value = String.format("%s,%s", String.valueOf(System.currentTimeMillis()), DateUtil.now());
            resource.set(key, value);
        }
    }

    private synchronized Integer createSixNum() {
        return RandomUtil.randomInt(100000, 999999);
    }

    public Integer getSixNum() {
        TimeInterval timer = DateUtil.timer();
        Set<Integer> oldNum = getOldNum();
        Integer sixNum = createSixNum();
        while (!oldNum.add(sixNum)) {
            sixNum = createSixNum();
        }
        saveNewNum(sixNum);
        log.info("生成6位不重复的纯数字,共用时{}毫秒", timer.interval());
        return sixNum;
    }*/
}
